<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * MAPI COA Project
 * login.php
 * @author ranuandra
 *
 * 4 Agu 2011
 * tags
 */

class Controller_Login extends Kohana_Mapi
{
	public $template = 'home';
	public function action_index()
	{
		
	    	try 
		{
			
						
			
			$user = ORM::factory('user');
			//$perusahaan = ORM::factory('perusahaan');	
			print_r($_POST);
			
			$user->create_user($_POST, array(
		 										'username',
												'password',
		 										'email',
		  									));
	  		$login_role = new Model_Role(array('name' =>'login'));
			$user->add('roles',$login_role);
	  		$login_role = new Model_Role(array('name' =>'admin'));
			$user->add('roles',$login_role);
			

			
			//$perusahaan->set('user_id', $user->id);
			//$perusahaan->create_perusahaan($_POST);
			
			//$email = Email::factory('test')->to();
			$msg = 'Login perusahaan berhasil dibuat, silahkan cek email anda untuk langkah selanjutnya';
			$this->template->bind('container', $msg);
			
		}
		catch (ORM_Validation_Exception $e) {
			$errors = $e->errors('models');
			print_r($errors);
			$view = View::factory('login/form');
			$this->template->bind('container', $view);
		}
//		if(isset($_POST['username']) and isset($_POST['password']))
//		{
//			$user = Auth_ORM::instance();
//			
//			//echo $_POST['password'];
//			//$status = Auth_ORM::instance()->login($_POST['username'], $_POST['password'], FALSE);
//			//echo "dsadasd";
//			//print_r($status);
//			$status = Auth_ORM::instance()->login($_POST['username'], $_POST['password'], FALSE);
//			
//			
//			if($status==1)
//			{
//				Request::current()->redirect(URL::base());
//			}
//			else 
//			{
//				Request::current()->redirect(URL::base());
//			}
//			
//			//print_r($status);
//			//exit;
//			//exit;
//				
//		}	
		
	}
	
}

?>